<template>
	<view class="indexs">
		<view class="header header_lan" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height"
				:style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo">
						<image :src="imgUrl+'/dzx_img22.png'" mode="widthFix"></image>
					</view>
					<view class="index_wenben baise">{{words.words.index.title.value?words.words.index.title.value:$t('page_dealer_apply.distribution_application')}}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<!-- 头部背景图 -->
		<view v-if="!words.is_applying" class="dealer-bg" :style="'padding-top:' + navP + 'px;'">
		  <image mode="widthFix" :src="words.background"></image>
		</view>
		  <!-- 填写申请信息 -->
		  <block v-if="!words.is_applying">
		    <view class="dis-flex flex-dir-column flex-y-center">
		      <view class="widget-form b-f m-top20 dis-flex flex-dir-column">
		        <view class="form-title f-30">{{ words.words.apply.words.title.value }}</view>
		        <view class="form-box dis-flex flex-dir-column">
		          <form report-submit="true">
		            <view class="form-field dis-flex flex-y-center">
		              <view class="field-label">{{ words.words.index.words.referee.value}}</view>
		              <view class="field-input">
		                <text>{{ words.referee_name }}（{{$t('page_dealer_apply.please_check')}}）</text>
		              </view>
		            </view>
		            <view class="form-field dis-flex flex-y-center">
		              <view class="field-label">{{ $t('page_dealer_apply.name') }}</view>
		              <view class="field-input flex-box">
		                <input type='text' :placeholder="$t('page_dealer_apply.real_name')" @input="nameFun"></input>
		              </view>
		            </view>
		            <view class="form-field dis-flex flex-y-center">
		              <view class="field-label">{{ $t('page_dealer_apply.phone_number') }}</view>
		              <view class="field-input flex-box">
		                <input type='digit' name="mobile" :placeholder="$t('page_dealer_apply.please_enter_phone_number')"  @input="mobileFun"></input>
		              </view>
		            </view>
		            <!-- 申请协议 -->
		            <view class="form-license dis-flex flex-x-center flex-y-center">
		              <view class="license-radio dis-flex flex-y-center" @tap="toggleSetRead">
		                <text :class='["license-icon","iconfont","f-38","icon-radio",is_read?"c-violet":"col-bb"]'></text>
		                <text class="f-28 col-80">{{ $t('page_dealer_apply.understand') }}</text>
		              </view>
		              <text @tap="toggleApplyLicense" class="f-28 c-violet">【{{ words.words.apply.words.license.value }}】</text>
		            </view>
		            <!-- 立即申请 -->
		            <view class="form-submit dis-flex flex-x-center">
		              <button @tap="onFormSubmit"   :disabled="disabled">{{ words.words.apply.words.submit.value }}</button>
		            </view>
		          </form>
		        </view>
		      </view>
		    </view>
		    <zan-dialog id="zan-base-dialog"></zan-dialog>
		  </block>
		  <!-- 等待审核 -->
		  <view v-if="words.is_applying" class="dealer-boot dis-flex flex-dir-column flex-y-center">
		    <view class="boot__msg f-30 dis-flex flex-dir-column flex-y-center">
		      <text class="msg__icon iconfont icon-shenhezhong"></text>
		      <text class="msg__content m-top20 f-29 col-80">{{ words.words.apply.words.wait_audit.value }}</text>
		    </view>
		    <!-- 去商城逛逛 -->
		    <view class="boot__submit form-submit dis-flex flex-x-center">
		      <form @tap="navigationToIndex" report-submit="true">
		        <button formType="submit">{{ words.words.apply.words.goto_mall.value }}</button>
		      </form>
		    </view>
		    <!-- 订阅消息通知 -->
		    <view class="boot__submsg" @tap="onSubMsg">
		        <text class="iconfont icon-iconxx"></text>
		        <text class="f-29">{{ $t('page_dealer_apply.notification') }}</text>
		    </view>
		  </view>
		
	</view>

</template>
<script>
const app = getApp();
let request = require('../../../../utils/require');

export default {
    data() {
        return {
			isData: false,
			words: {},
			user: {},
			dealer: {},
			imgUrl:app.globalData.imgUrl,
			navH: app.globalData.navHeight,
			navP:40,
			gaodu: app.globalData.height,
			banertrue: false,
			is_read: false,
			disabled: false,
			submsgSetting: {}, // 订阅消息配置
			username:'',
			mobile:0,
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
		var that = this;
		that.getSubmsgSetting();
    },
    onShow() {
	 var that = this;
		// 获取分销商中心数据
		that.getApplyState();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.ispage) {
            this.page = this.page + 1
            this.baoguolist(this.type);
        }
    },
	onPageScroll: function(e) {
		if (e.scrollTop > this.navH + 50) {
			this.banertrue = true
		} else {
			this.banertrue = false
		}
	}, 
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
	
	 
	 methods: {
		 //物品价值
		 nameFun(e) {
		     this.username= e.detail.value
		 },
		 mobileFun(e){
			 this.mobile = e.detail.value
		 },
	  /**
	   * 获取订阅消息配置
	   */
	  getSubmsgSetting() {
	    var that = this;
		var data = {
			token: uni.getStorageSync('token')
		};
		request.get(`index.php?s=/api/wxapp.submsg/setting&wxapp_id=` + uni.getStorageSync('web_id'),data).then((res) => {
	      that.submsgSetting = res.data.data.setting
		  console.log(res,99979);
	    });
	  },
	    /**
	     * 获取分销商申请状态
	     */
	    getApplyState() {
	     var that = this;
		 var token = {
		 	token: uni.getStorageSync('token'),
			referee_id: that.getRefereeid()
		 };
		 
		 request.get(`index.php?s=/api/user.dealer/apply&wxapp_id=` + uni.getStorageSync('web_id'),token).then((ress) => {
			  console.log(ress,76543);
	        var data = ress.data.data;
			that.words = data;
	        // 当前是否已经为分销商
	        if (ress.data.data.is_dealer) {
	          uni.redirectTo({
	            url: '/pages/mys/my_fenxiao/my_fenxiao'
	          });
	        }
	        // 设置当前页面标题
	        // uni.setNavigationBarTitle({
	        //   title: that.words.apply.title.value
	        // });
	        that.isData = true;
	        
	      });
	    },
		  	
		  /**
		   * 获取推荐人id
		   */
		getRefereeid() {
		  return uni.getStorageSync('referee_id');
		},
		jumpzuo() {
		    uni.navigateBack()
		},
		/**
		   * 显示申请协议
		   */
		  toggleApplyLicense() {
			var that = this;
			console.log(that.words.words.apply.words.license.value);
			uni.showModal({
				title:that.words.words.apply.words.license.value,
				content: that.words.license,
				confimrText:'我已阅读',
				showCancel:false,
				success: function (res) {
					if (res.confirm) {
						console.log('我已阅读');
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});			
		  },  
		  /**
		   * 已阅读
		   */
		  toggleSetRead() {
		    var that = this;
		    that.is_read= !that.is_read
		  }, 
		   /**
		     * 订阅消息通知
		     */
		    onSubMsg() {
		      var that = this;
		      var tmplItem = that.submsgSetting.dealer.apply.template_id;
		      if (tmplItem.length > 0) {
		        uni.requestSubscribeMessage({
		          tmplIds: [tmplItem],
		          success(res) {},
		          fail(res) {},
		          complete(res) {},
		        });
		      }
		    },
		  /**
		   * 提交申请 
		   */
		  onFormSubmit(e) {
			  console.log(8888);
		    var that = this;
			var datas = {
				token: uni.getStorageSync('token'),
				mobile:that.mobile,
				username:that.username,
			};
		    // 验证姓名
		    if (!datas.username || datas.username.length < 1) {
		      uni.showToast({
		      	title:'请填写姓名'
		      });
		      return false;
		    }
		
		    // 验证手机号
		    if (!/^\+?\d[\d -]{8,12}\d/.test(datas.mobile)) {
			  uni.showToast({
			  	title:'手机号格式不正确'
			  });
		      return false;
		    }
		
		    // 验证是否阅读协议
		    if (!that.is_read) {
			  uni.showToast({
			  	title:'请先阅读协议'
			  });
		      return false;
		    }
		
		    // 按钮禁用
		    that.disabled= true
		
		    // 数据提交
			request.post(`index.php?s=/api/user.dealer.apply/submit&wxapp_id=` + uni.getStorageSync('web_id'),datas).then((res) => {
		      that.getApplyState();
		    }, null, () => {
		      // 解除按钮禁用
		      that.disabled= false
		    });
		  }, 
		//跳转首页
		navigationToIndex(e) {
		    // 跳转到首页
		    uni.reLaunch({
		    	   url: '/pages/index/index',
		    })
		  },
	 }
};
</script>
<style>
	@import './apply.css';
</style>